<?php

use App\Constants\BaseConstants;
use \Illuminate\Contracts\Pagination\LengthAwarePaginator;
use \App\Exceptions\ServiceException;
use \App\Constants\ErrorMsgConstants;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Ramsey\Uuid\Uuid;


/**
 * 解析异常信息
 * @param Exception $e
 * @return array
 */
function getExceptionInfo(Exception $e)
{
    return [
        "Code" => $e->getCode(),
        "Message" => $e->getMessage(),
        "File" => $e->getFile(),
        "Line" => $e->getLine(),
    ];
}


/**
 * 记录日志
 * @param $logName
 * @return Logger
 */
function customerLoggerHandle($logName)
{
    $logName = $logName . "-" . exec('whoami');
    $log = new Logger($logName);
    $logFilePath = storage_path('logs') . "/" . $logName . ".log";
    $log->pushHandler(new RotatingFileHandler($logFilePath, 0, Logger::DEBUG));

    return $log;
}

/**
 * 通用签名方法
 * @param array $data 签名数据
 * @param string $signKey 签名KEY
 * @return mixed
 */
function signServiceRequestData(array $data, string $signKey)
{
    unset($data['sign']);
    ksort($data);
    $sign = md5(customBuildQuery($data) . $signKey);

    return $sign;
}

/**
 * 签名字符串拼接
 * @param array $data
 * @return string
 */
function customBuildQuery(array $data)
{
    unset($data['sign']);
    ksort($data);
    $list = [];
    foreach ($data as $key => $value) {
        if (!is_null($value)) {
            if (is_array($value) || is_object($value)) {
                $value = json_encode($value);
            } elseif (is_bool($value)) {
                $value = $value ? 'true' : 'false';
            }
            $list[] = $key . "=" . $value;
        }
    }

    customerLoggerHandle("sign")->debug("sign", [join("&", $list)]);

    return join("&", $list);
}


/**
 * 积分转换等额金额
 * @param $point
 * @return string
 */
function transformPointToYuan($point)
{
    if ($point <= 0) {
        return 0;
    }

    $pointPrice = ($point / 1000);

    return number_format($pointPrice, 2, ".", "");
}

/**
 * 等额金额转换积分
 * @param $price
 * @return string
 */
function transformYuanToPoint($price)
{
    if ($price <= 0) {
        return 0;
    }

    $point = $price / BaseConstants::POINT_TO_YUAN;

    return $point;
}

/**
 * 价格元转换成分
 * @param $price
 * @return int
 */
function transformYuanToCent($price)
{
    if ($price <= 0) {
        return 0;
    }
    return (int)number_format($price * 100, 0, ".", "");
}

/**
 * 分(单位:分)转金额(单位元)
 * @param $price
 * @return string
 */
function transformCentToYuan($price)
{
    if ($price <= 0) {
        return 0;
    }

    return number_format($price / 100, 2, ".", "");
}

/**
 * 获取一个uuid
 * @return string
 * @throws Exception
 */
function generateNewUuid()
{
    return Uuid::uuid4()->toString();
}

/**
 * @param LengthAwarePaginator $models
 * @return array
 */
function getPageInfo(LengthAwarePaginator $models)
{
    return [
        'currentPage' => $models->currentPage(),
        'perPage' => $models->perPage(),
        'total' => $models->total(),
        'lastPage' => $models->lastPage(),
    ];
}



function newOrderNo()
{
    return getMicroTime() . rand(11111, 99999);
}


function getMicroTime()
{
    list($mSec, $sec) = explode(' ', microtime());
    $microTime = (float)sprintf('%.0f', (floatval($mSec) + floatval($sec)) * 1000);
    return $microTime;
}

function getAppUser()
{
    try {
        /** @var \App\Models\AppUser $appUser */
        $appUser = auth('api')->user();
        return $appUser;
    } catch (Exception $exception) {
        throw new ServiceException(ErrorMsgConstants::TOKEN_ERROR, "登录失败!请重新登录!");

    }
}

/**
 * 解析异常信息
 * @param Exception $e
 * @return array
 */
function getExceptionMainInfo(Exception $e)
{
    return [
        "Code"    => $e->getCode(),
        "Message" => $e->getMessage(),
        "File"    => $e->getFile(),
        "Line"    => $e->getLine(),
    ];
}









